package com.shoy.myapp.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.shoy.myapp.app.AppContext;

/**
 * Fragment基类
 *
 * @author shay
 * @created 2015/04/03
 */
@SuppressLint("NewApi")
public abstract class BaseFragment extends Fragment implements IBaseFragment {
    /**
     * 当前Fragment渲染的视图View*
     */
    private View mContextView = null;
    /**
     * 共通操作*
     */
    private Operation mBaseOperation = null;
    protected AppContext mApplication;
    /**
     * 日志输出标志*
     */
    protected final String TAG = this.getClass().getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //渲染视图View(防止切换时重绘View)
        if (null != mContextView) {
            ViewGroup parent = (ViewGroup) mContextView.getParent();
            if (null != parent) {
                parent.removeView(mContextView);
            }
        } else {
            mContextView = inflater.inflate(bindLayout(), null);
            // 控件初始化
            initView(mContextView);

            //实例化共通操作
            mBaseOperation = new Operation(getActivity());
        }
        mApplication = (AppContext) getContext().getApplicationContext();

        //业务处理
        doBusiness(getActivity());

        return mContextView;
    }

    @Override
    public void doBusiness(Context context) {
        business(context);
    }

    protected void business(Context context) {

    }

    protected View getContextView() {
        return mContextView;
    }

    /**
     * 获取当前Fragment依附在的Activity
     */
    protected Activity getContext() {
        return getActivity();
    }

    /**
     * 获取共通操作机能
     */
    public Operation getOperation() {
        return this.mBaseOperation;
    }
}
